package club.gskj.gulimall.product.service.impl;

import club.gskj.gulimall.product.dao.BrandDao;
import club.gskj.gulimall.product.dao.CategoryDao;
import club.gskj.gulimall.product.entity.BrandEntity;
import club.gskj.gulimall.product.entity.CategoryEntity;
import club.gskj.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.PortUnreachableException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.gskj.common.utils.PageUtils;
import club.gskj.common.utils.Query;

import club.gskj.gulimall.product.dao.CategoryBrandRelationDao;
import club.gskj.gulimall.product.entity.CategoryBrandRelationEntity;
import club.gskj.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {

        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        // 获取品牌name
        BrandEntity brandEntity = brandDao.selectById(brandId);
        String brandName = brandEntity.getName();

        // 获取分类name
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        String categoryEntityName = categoryEntity.getName();

        // 保存到中间表
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(categoryEntityName);

        this.save(categoryBrandRelation);
    }

    /**
     * 当更新 brand中的字段 有存放在中间表时，也更新中间表中的字段。 保持数据一致性
     */
    @Override
    public void updateBrand(Long brandId, String name) {

        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        // 品牌id
        entity.setBrandName(name);
        // 品牌name
        entity.setBrandId(brandId);

        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("brand_id", brandId);
        this.update(entity, wrapper);
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {

        List<CategoryBrandRelationEntity> entityList = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        return entityList.stream().map(item -> {
            return brandService.getById(item.getBrandId());
        }).collect(Collectors.toList());
    }
}
